package com.softer.chengxin.scheduled;

import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.scheduled.dao.ScheduledTaskDao;
import com.softer.chengxin.score.dao.SDManagerDao;
import com.softer.chengxin.score.entity.ScoreComposite;
import com.softer.chengxin.score.util.ScoreUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/1/18
 */
public class TaskRunnable implements Runnable {

    @Override
    public void run() {
        //实现Runnable接口，自动注入失败，改为工具类获取
        ScheduledTaskDao scheduledTaskDao = ApplicationContextProvider.getBean(ScheduledTaskDao.class);
        ScoreUtil scoreUtil = ApplicationContextProvider.getBean(ScoreUtil.class);
        SDManagerDao managerDao = ApplicationContextProvider.getBean(SDManagerDao.class);

        //任务逻辑代码部分.
        //获取年度
        String year = DateFormatUtil.getSysYear();
        //查询各个企业的有效优良总得分和负面总得分
        List<ScoreComposite> list = scheduledTaskDao.getScoreCompositeList();
        for (ScoreComposite sc : list) {
            BigDecimal basicScore = scoreUtil.getBasicScore(sc.getCompanyId(),sc.getExcellentScore(),sc.getNegativeScore(),year);
            sc.setBasicScore(basicScore);
            //计算总得分
            BigDecimal allScore = basicScore.add(sc.getExcellentScore()).subtract(sc.getNegativeScore());
            sc.setAllScore(allScore);
            String level = scoreUtil.getLevel(allScore, sc.getCompanyId());
            sc.setLevel(level);
            sc.setYear(year);
            managerDao.insertScoreComposite(sc);
        }
    }
}
